package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: sky-take-out
 * @description:
 * @author: lydms
 * @create: 2024-03-05 17:05
 **/

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "商家订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 取消订单
     *
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("取消订单号：{}", ordersCancelDTO.getId());

        orderService.cancel(ordersCancelDTO);

        return Result.success();
    }


    /**
     * 查询列表订单
     *
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("查询列表订单")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("查询列表订单");
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 统计各订单的数量
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("统计各订单的数量")
    public Result<OrderStatisticsVO> statistics(){

      OrderStatisticsVO orderStatisticsVO = orderService.statistics();

        return Result.success(orderStatisticsVO);
    }


    /**
     * 查看订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查看订单详情")
    public Result<OrderVO> details(@PathVariable Long id){
        log.info("查看订单详情：{}",id);
        OrderVO orderVO = orderService.detail(id);
        return Result.success(orderVO);

    }

    /**
     * 商家接单
     * @param ordersConfirmDTO
     * @return
     */
    @PutMapping("/comfirm")
    @ApiOperation("商家接单")
    public Result comfirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO){

        log.info("商家接单：{}",ordersConfirmDTO.getStatus());
        orderService.comfirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 商家拒单
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("商家拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("商家拒单：{}",ordersRejectionDTO.getId());
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }


    /**
     * 商家派送
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("商家派送")
    public Result delivery(@PathVariable Long id){
        log.info("商家派送：{}",id);
        orderService.delivery(id);
        return Result.success();
    }


    /**
     * 完成订单
     *
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable("id") Long id) {
        orderService.complete(id);
        return Result.success();
    }
}
